1
|
|
|
/* |
2
|
|
|
* read-bytes: Function to read data from bytes. |
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
4
|
|
|
* https://github.com/rochars/byte-data |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
let helpers = require("../src/helpers.js"); |
8
|
|
|
const floats = require("../src/floats.js"); |
9
|
|
|
const intBits = require("int-bits"); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Read a group of bytes by turning it to bits. |
13
|
|
|
* Useful for 40 & 48-bit, but underperform. |
14
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
15
|
|
|
* @param {number} i The index to read. |
16
|
|
|
* @param {number} numBytes The number of bytes |
17
|
|
|
* (1 for 8-bit, 2 for 16-bit, etc). |
18
|
|
|
* @return {number} |
19
|
|
|
*/ |
20
|
|
|
function readBytesAsBits(bytes, i, numBytes) { |
21
|
|
|
let j = numBytes-1; |
22
|
|
|
let bits = ""; |
23
|
|
|
while (j >= 0) { |
24
|
|
|
bits += helpers.bytePadding(bytes[j + i].toString(2), 2); |
25
|
|
|
j--; |
26
|
|
|
} |
27
|
|
|
return parseInt(bits, 2); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Read 1 8-bit int from from bytes. |
32
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
33
|
|
|
* @param {number} i The index to read. |
34
|
|
|
* @return {number} |
35
|
|
|
*/ |
36
|
|
|
function read8Bit(bytes, i) { |
37
|
|
|
return bytes[i]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Read 1 16-bit int from from bytes. |
42
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
43
|
|
|
* @param {number} i The index to read. |
44
|
|
|
* @return {number} |
45
|
|
|
*/ |
46
|
|
|
function read16Bit(bytes, i) { |
47
|
|
|
return bytes[1 + i] << 8 | bytes[i]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Read 1 16-bit float from from bytes. |
52
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
53
|
|
|
* @param {number} i The index to read. |
54
|
|
|
* @return {number} |
55
|
|
|
*/ |
56
|
|
|
function read16BitFloat(bytes, i) { |
57
|
|
|
return floats.decodeFloat16(bytes.slice(i,i+2)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Read 1 24-bit int from from bytes. |
62
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
63
|
|
|
* @param {number} i The index to read. |
64
|
|
|
* @return {number} |
65
|
|
|
*/ |
66
|
|
|
function read24Bit(bytes, i) { |
67
|
|
|
return bytes[2 + i] << 16 | |
68
|
|
|
bytes[1 + i] << 8 | |
69
|
|
|
bytes[i]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Read 1 32-bit int from from bytes. |
74
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
75
|
|
|
* @param {number} i The index to read. |
76
|
|
|
* @return {number} |
77
|
|
|
*/ |
78
|
|
|
function read32Bit(bytes, i) { |
79
|
|
|
return (bytes[3 + i] << 24 | |
80
|
|
|
bytes[2 + i] << 16 | |
81
|
|
|
bytes[1 + i] << 8 | |
82
|
|
|
bytes[i]) >>> 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Read 1 32-bit float from from bytes. |
87
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
88
|
|
|
* @param {number} i The index to read. |
89
|
|
|
* @return {number} |
90
|
|
|
*/ |
91
|
|
|
function read32BitFloat(bytes, i) { |
92
|
|
|
return intBits.pack(read32Bit(bytes, i)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Read 1 40-bit int from from bytes. |
97
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
98
|
|
|
* @param {number} i The index to read. |
99
|
|
|
* @return {number} |
100
|
|
|
*/ |
101
|
|
|
function read40Bit(bytes, i) { |
102
|
|
|
return readBytesAsBits(bytes, i, 5); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Read 1 48-bit int from bytes. |
107
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
108
|
|
|
* @param {number} i The index to read. |
109
|
|
|
* @return {number} |
110
|
|
|
*/ |
111
|
|
|
function read48Bit(bytes, i) { |
112
|
|
|
return readBytesAsBits(bytes, i, 6); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Read 1 64-bit double from bytes. |
117
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
118
|
|
|
* @param {number} i The index to read. |
119
|
|
|
* @return {number} |
120
|
|
|
*/ |
121
|
|
|
function read64BitFloat(bytes, i) { |
122
|
|
|
return floats.decodeFloat64(bytes.slice(i,i+8)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Read 1 char from bytes. |
127
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
128
|
|
|
* @param {number} i The index to read. |
129
|
|
|
* @return {string} |
130
|
|
|
*/ |
131
|
|
|
function readChar(bytes, i, type) { |
132
|
|
|
let chrs = ""; |
133
|
|
|
let j = 0; |
134
|
|
|
let len = type.bits / 8; |
135
|
|
|
while(j < len) { |
136
|
|
|
chrs += String.fromCharCode(bytes[i+j]); |
137
|
|
|
j++; |
138
|
|
|
} |
139
|
|
|
return chrs; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
module.exports.readChar = readChar; |
143
|
|
|
module.exports.read8Bit = read8Bit; |
144
|
|
|
module.exports.read16Bit = read16Bit; |
145
|
|
|
module.exports.read16BitFloat = read16BitFloat; |
146
|
|
|
module.exports.read24Bit = read24Bit; |
147
|
|
|
module.exports.read32Bit = read32Bit; |
148
|
|
|
module.exports.read32BitFloat = read32BitFloat; |
149
|
|
|
module.exports.read40Bit = read40Bit; |
150
|
|
|
module.exports.read48Bit = read48Bit; |
151
|
|
|
module.exports.read64BitFloat = read64BitFloat; |
152
|
|
|
|